package com.fw.service.inboundmessagelog.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.inboundmessagelog.InboundMessageLogService;
import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.persistence.dao.InboundMessageLogDAO;
import com.fw.service.persistence.dto.InboundMessageLog;
import com.fw.util.DateUtils;
import com.fw.util.StringUtils;

public class InboundMessageLogServiceImpl implements InboundMessageLogService {

    private static Logger log = Logger.getLogger(InboundMessageLogServiceImpl.class.getName());

    @Autowired
    private InboundMessageLogDAO inboundMessageLogDAO;

    @Override
    public InboundMessageLogBean createInboundMessageLog(InboundMessageLogBean inboundMessageLogBean) {
	InboundMessageLogBean returnInboundMessageLogBean = null;
	try {
	    if (inboundMessageLogBean != null) {
		InboundMessageLog inboundMessageLog = new InboundMessageLog();
		BeanUtils.copyProperties(inboundMessageLogBean, inboundMessageLog);
		Date currentDate = DateUtils.getCurrentDate();
		inboundMessageLog.setDateTime(StringUtils.formatDate(currentDate, StringUtils.PATTERN_DATETIME_PERSISTENCE));
		inboundMessageLog = inboundMessageLogDAO.save(inboundMessageLog);
		returnInboundMessageLogBean = new InboundMessageLogBean();
		BeanUtils.copyProperties(inboundMessageLog, returnInboundMessageLogBean);
		returnInboundMessageLogBean.setId(inboundMessageLog.getId());
	    }
	} catch (Exception e) {
	    log.error(e, e);
	}
	return returnInboundMessageLogBean;
    }

    @Override
    public InboundMessageLogBean getInboundMessageLog(Long id) {
	InboundMessageLogBean returnInboundMessageLogBean = null;
	try {
	    InboundMessageLog inboundMessageLog = inboundMessageLogDAO.findOne(id);
	    if (inboundMessageLog != null) {
		returnInboundMessageLogBean = new InboundMessageLogBean();
		BeanUtils.copyProperties(inboundMessageLog, returnInboundMessageLogBean);
		returnInboundMessageLogBean.setId(inboundMessageLog.getId());
	    }
	} catch (Exception e) {
	    log.error(e, e);
	}
	return returnInboundMessageLogBean;
    }

}
